package joc.contract;

import joc.customize.Contract;
import joc.customize.ForMethodEvaluateAtEndOfMethodWithReturnValue;
import joc.customize.ObjectAwareContract;
import joc.customize.ParameterAwareContract;
import joc.customize.PartialEvaluationResult;

public class EqualsContract extends Contract<Equals> implements ObjectAwareContract, ParameterAwareContract {
	@ForMethodEvaluateAtEndOfMethodWithReturnValue
	public PartialEvaluationResult checkContract(Equals annotation, boolean isEqual, Object object, Object param) {
		if (param == null && isEqual) {
			return getFailure("Equals must return false for parameter null.");
		}
		if (isEqual && object.hashCode() != param.hashCode()) {
			return getFailure("For equal objects, hashCode must return the same value.");
		}
		if (!object.equals(object)) {
			return getFailure("Equals must be reflexive.");
		}
		if (param != null && object.equals(param) != param.equals(object)) {
			return getFailure("Equals must be symmetric.");
		}
		return getSuccess();
	}
}